let Node = require('./node');
let Matrix = require('./matrix');
let RenderItem = require('./renderItem');
let cfg = require('./utils/cfg');
let Vec2 = require('./vec2');

class Sprite extends Node {

    constructor(obj = {}) {
        super();

        this._x = obj.x || 0;
        this._y = obj.y || 0;
        this._sx = obj.sx || 1;
        this._sy = obj.sy || 1;
        this._degress = obj.degress || 0;
        this._zIndex = obj.zIndex || 0;
        this._opacity = (obj.opacity === undefined)?1.0:obj.opacity;

        // ax ay
        this._ax = (obj.ax === undefined)?0.5:obj.ax;
        this._ay = (obj.ay === undefined)?0.5:obj.ay;

        // color
        if (obj.color) {
            this._color.set(obj.color);
        }

        // texture width height
        this._texture = obj.texture;
        if (this._texture) {
            // 判断是否指定了宽高
            if (obj.width && obj.height) {
                this._width = obj.width;
                this._height = obj.height;
            } else {
                this._width = this._texture.width;
                this._height = this._texture.height;
            }
        } else {
            //  给纹理设置一个默认纹理
            this._texture = cfg.defaultTexture;
            this._width = obj.width || 100;
            this._height = obj.height || 100;
        }

        this._shader = cfg.defaultShader;
        this._initIBO();
    }

    get texture() {
        return this._texture;
    }

    updateTexture(newTexture, bUpdateSize = false) {
        this._texture = newTexture;
        if (newTexture) {
            if (bUpdateSize) {
                this.width = newTexture.width;
                this.height = newTexture.height;
            }
        }
    }

    getRenderItem() {

        let renderItem = new RenderItem();
        Matrix.mul(this._mvpMat, cfg.pvMatrix, this._worldMat);

        renderItem.set({
            vbo: this._vbo,
            ibo: this._ibo,
            count: 6,
            drawMode: this._gl.TRIANGLES,
            shader: this._shader,
            texture: this._texture,
            mvpMat: this._mvpMat,
            color: this.color,
            opacity:this.opacity
        });
        return renderItem;
    }

    _initIBO() {
        let gl = this._gl;
        let indices = [0, 1, 2, 0, 2, 3];
        let ibo = this._ibo = gl.createBuffer();
        gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, ibo);
        gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), gl.STATIC_DRAW);
        gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
        return ibo;
    }

    onVerticesChanged() {

        let gl = this._gl;

        let width = this._width;
        let height = this._height;

        let ax = this._ax;
        let ay = this._ay;

        // 直接使用一张默认的灰色纹理
        let vertices = [
            -width * ax, -height * ay, 0, 1,
            width * (1 - ax), -height * ay, 1, 1,
            width * (1 - ax), height * (1 - ay), 1, 0,
            -width * ax, height * (1 - ay), 0, 0,
        ];
        let vbo;
        if (this._vbo) {
            vbo = this._vbo;
        } else {
            vbo = this._vbo = gl.createBuffer();
        }
        gl.bindBuffer(gl.ARRAY_BUFFER, vbo);
        gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
        gl.bindBuffer(gl.ARRAY_BUFFER, null);
    }

    dispose() {
        super.dispose();
        let gl = this._gl;
        if (this._ibo) {
            gl.deleteBuffer(this._ibo);
            this._ibo = null;
        }
        if (this._vbo) {
            gl.deleteBuffer(this._vbo);
            this._vbo = null;
        }
    }

    containPos(pos) {

        let width = this.scaledWidth;
        let height = this.scaledHeight;

        let min_x = -this._ax * width;
        let min_y = -this._ay * height;
        let max_x = (1 - this._ax) * width;
        let max_y = (1 - this._ay) * height;

        return pos.x >= min_x && pos.y >= min_y &&
               pos.x <= max_x && pos.y <= max_y;

        // let width = this.width * this.sx;
        // let height = this.height * this.sy;
        // let x = this.x;
        // let y = this.y;
        // return (pos.x >= x - width / 2 &&
        //     pos.x <= x + width / 2 &&
        //     pos.y >= y - height / 2 &&
        //     pos.y <= y + height / 2);
    }

    getLeftBottom(){
        let x = this.x - this.ax * this.scaledWidth;
        let y = this.y - this.ay * this.scaledHeight;
        return new Vec2(x,y);
    }
}



module.exports = Sprite;
